#!/bin/bash

set -o errexit
set -o pipefail

export BASE_DATA_PATH=/home/mixagol/data
export BASE_DATE=20120802
export CUR_DIR=${BASE_DATA_PATH}/0_src/${BASE_DATE}
export LC_ALL=C

mkdir -p ${CUR_DIR}

wget 'ftp://ftp.ncbi.nlm.nih.gov/genomes/Bacteria/all.gbk.tar.gz' -O "${CUR_DIR}/all.gbk.tar.gz"

mkdir -p ${CUR_DIR}/data_raw
tar -C ${CUR_DIR}/data_raw -xzf ${CUR_DIR}/all.gbk.tar.gz

#grep -r "DEFINITION .* complete genome" ${CUR_DIR}/data_raw | cut -d: -f1 | uniq | LC_ALL=C sort > ${CUR_DIR}/complete_genomes.txt

for fname in `find ${CUR_DIR}/data_raw/ -type f`; do
    FOUND=$(cat $fname | sed ':a;N;$!ba;s/\n            / /g' | head -n100 | grep -r "^DEFINITION .* complete genome" | wc -l)
    if [ $FOUND -ge 1 ] ; then
        echo $fname
    fi
done \
| sort -u > ${CUR_DIR}/complete_genomes.txt

cat ${CUR_DIR}/complete_genomes.txt \
    | xargs stat -c %s$'\t'%n \
    | awk -F '/' '{split($(NF-1), M, "_"); split($NF, N, "."); split($0, S, "\t"); print M[1]"_"M[2]"\t"S[1]"\t"S[2]}' \
    | sort -t$'\t' -k2,2 -g -r \
    | sort -t$'\t' -k1,1 -s -u \
    | cut -f3 \
    > ${CUR_DIR}/complete_genomes_uniq.txt

cat ${CUR_DIR}/complete_genomes.txt \
    | xargs stat -c %s$'\t'%n \
    | awk -F '/' '{split($(NF-1), M, "_"); split($NF, N, "."); split($0, S, "\t"); print substr(M[1], 0, 4)"\t"S[1]"\t"S[2]}' \
    | sort -t$'\t' -k2,2 -g -r \
    | sort -t$'\t' -k1,1 -s -u \
    | cut -f3 \
    | awk '{n=split($1, M, "/"); n=split(M[n], M, "."); print M[1]"\t"$1}' \
    > ${CUR_DIR}/complete_genomes_uniq_l0.txt

cat ${CUR_DIR}/complete_genomes.txt \
    | xargs stat -c %s$'\t'%n \
    | awk -F '/' '{split($(NF-1), M, "_"); split($NF, N, "."); split($0, S, "\t"); print M[1]"\t"S[1]"\t"S[2]}' \
    | sort -t$'\t' -k2,2 -g -r \
    | sort -t$'\t' -k1,1 -s -u \
    | cut -f3 \
    | awk '{n=split($1, M, "/"); n=split(M[n], M, "."); print M[1]"\t"$1}' \
    > ${CUR_DIR}/complete_genomes_uniq_l1.txt


# 0_src/create_raw_db.py ${CUR_DIR}/raw.db ${CUR_DIR}/complete_genomes_uniq.txt

mkdir -p ${CUR_DIR}/orgs
0_src/create_orgs_dir.py ${CUR_DIR}/complete_genomes_uniq.txt ${CUR_DIR}/orgs

rm -rf data_raw


